package com.zhida.smartcontrol.dialog.grouplistdialog;

import android.app.Dialog;
import android.os.Bundle;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.jay.tools.recycler.OnItemClick;
import com.zhida.smartcontrol.R;
import com.zhida.smartcontrol.mvp.MVPBaseDialogFragment;
import com.zhida.smartcontrol.pagectrl.projectdetail.GroupBean;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * author: tanjianpei
 * date: 2019/12/16 016 16:52
 * description: 选择区段
 */
public class GroupListDialogFragment extends MVPBaseDialogFragment<GroupListDialogContract.View, GroupListDialogPresenter> implements GroupListDialogContract.View {
    @BindView(R.id.recycler_view)
    RecyclerView mRView;

    private GroupListAdapter mAdapter;
    private GroupBean mBean;

    public static GroupListDialogFragment newInstance(String projectId) {
        Bundle args = new Bundle();
        args.putString("id", projectId);
        GroupListDialogFragment fragment = new GroupListDialogFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected Object getLayout() {
        mShowBottom = true;
        return R.layout.dialog_common_sel;
    }

    @Override
    protected void initDatas(Bundle savedInstanceState) {
        mRView.setLayoutManager(new LinearLayoutManager(mActivity));
        mAdapter = new GroupListAdapter(null);
        mAdapter.bindToRecyclerView(mRView);

        mPresenter.requestGroupList(getArguments().getString("id"));
    }

    @OnClick({R.id.btn_cancel, R.id.btn_confirm})
    public void click(View v) {
        switch (v.getId()) {
            case R.id.btn_cancel:
                dismiss();
                break;
            case R.id.btn_confirm:
                if (null != mListener) {
                    mListener.onDestroy(mBean, false);
                    dismiss();
                }
                break;
        }
    }

    @Override
    protected void initEvents(Dialog dialog) {
        mAdapter.setClickListener(new OnItemClick<GroupBean>() {
            @Override
            public void onClick(GroupBean info, int tag) {
                mBean = info;
            }
        });
    }

    @Override
    public void showGroup(List<GroupBean> groupList) {
        mAdapter.replaceData(groupList);
    }
}
